<template>  
    <view class="container">  
		<view class="user-section">
			<image v-if="hasLogin" class="bg" :src="userInfo.avatar_info.url"></image>
			<image v-else class="bg"  src="/static/user-bg.jpg"></image>
			<view class="user-info-box">
				<view class="portrait-box">
					<image v-if="hasLogin" class="portrait" :src="userInfo.avatar_info.url"></image>
					<image v-else class="portrait" src="/static/missing-face.png"></image>
				</view>
				<view class="info-box">
					<text class="username">{{userInfo.name || '游客,请登录'}}</text>
				</view>
			</view>
			<view class="vip-card-box">
				<image class="card-bg" src="/static/vip-card-bg.png" mode=""></image>
				<!--<view class="b-btn" v-if="userInfo.school">-->
					 <!--{{userInfo.school}}-->
				<!--</view>-->
				<view class="tit" v-if="hasLogin">
					<text class="yticon">余额</text>
					{{userInfo.money}}
				</view>
				<text class="e-m"></text>
				<text class="e-b"></text>
			</view>
		</view>
		
			<view 
				class="cover-container"
				:style="[{
					transform: coverTransform,
					transition: coverTransition
				}]"
				@touchstart="coverTouchstart"
				@touchmove="coverTouchmove"
				@touchend="coverTouchend"
			>
			<image class="arc" src="/static/arc.png"></image>

			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/order/order_list?tbIndex=0')">
					<text class="yticon icon-shouye"></text>
					<text>全部订单</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order_list?tbIndex=3')">
					<text class="yticon icon-daifukuan"></text>
					<text>已付款/待发货</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order_list?tbIndex=4')">
					<text class="yticon icon-yishouhuo"></text>
					<text>已发货</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order_list?tbIndex=5')">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>完成</text>
				</view>
			</view>

			<view class="list-cell b-b m-t" @click="navTo('/pages/userinfo/userinfo')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">个人资料</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b" @click="navTo('/pages/address/address-list')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">收货地址</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell" @click="navTo('/pages/user/recharge')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">充值中心</text>
				<text class="cell-more yticon icon-you"></text>
			</view>

			<view class="list-cell b-b m-t" @click="navTo('/pages/order/order_ship')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">等待我发货</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b" @click="navTo('/pages/goods/add_goods')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">发布商品</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell b-b" @click="navTo('/pages/user/recharge_log')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">充值记录</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			<view class="list-cell" @click="navTo('/pages/goods/manager_goods')" hover-class="cell-hover" :hover-stay-time="50">
				<text class="cell-tit">商品管理</text>
				<text class="cell-more yticon icon-you"></text>
			</view>
			
			<view class="list-cell log-out-btn" v-if="hasLogin" @click="toLogout">
				<text class="cell-tit">退出登录</text>
			</view>
		</view>
			
		
    </view>  
</template>  
<script>
    import { mapState, mapMutations } from 'vuex'
	import { login, client } from '../../common/config'

	let startY = 0, moveY = 0, pageAtTop = true;
    export default {
		data(){
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
			}
		},
		onLoad(){
		},
		created() {
		},
        computed: {
			...mapState(['hasLogin','userInfo']),
		},
        onPullDownRefresh() {
            //监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
            this.getUserInfo();
        },
        methods: {
			...mapMutations(['login','logout']),
			//每次进入这个页面就要获得用户数据，从服务器上更新
			getUserInfo(){
                if(this.hasLogin) {
                    this.$api.MyRequest(client.GetClient.url, client.GetClient.method)
                        .then((res) => {
                            this.login(res)
                        })
                        .catch((e) => {
                        })
                }
                uni.stopPullDownRefresh();//停止下拉刷新
			},
			//退出登录
            toLogout(){
                uni.showModal({
                    title: '提示',
                    content: '确定要退出登录么？',
                    success:  (res) => {
                        if (res.confirm) {
                            this.goToLogout();
                        } else if (res.cancel) {
                        }
                    }
                });
			},
            async goToLogout(){
                await this.$api.MyRequest(login.DeleteLogout.url, login.DeleteLogout.method, {})
                    .then(res => {
                        this.logout()
                    })
                    .catch(err => {
                    });
                //如果只打包在一个平台使用，线上要把这句话注释了。
                this.logout()
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url){
				if(!this.hasLogin){
					url = '/pages/public/login';
				}
				uni.navigateTo({  
					url
				})  
			}, 
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e){
				if(pageAtTop === false){
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e){
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if(moveDistance < 0){
					this.moving = false;
					return;
				}
				this.moving = true;
				if(moveDistance >= 80 && moveDistance < 100){
					moveDistance = 80;
				}
		
				if(moveDistance > 0 && moveDistance <= 80){
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend(){
				if(this.moving === false){
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
        }  
    }  
</script>  
<style lang='scss'>
	page{
		background: #f5f5f5;
	}
	%flex-center {
	 display:flex;
	 flex-direction: column;
	 justify-content: center;
	 align-items: center;
	}
	%section {
	  display:flex;
	  justify-content: space-around;
	  align-content: center;
	  background: #fff;
	  border-radius: 10upx;
	}

	.user-section{
		height: 520upx;
		padding: 100upx 30upx 0;
		position:relative;
		.bg{
			position:absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .4;
		}
	}
	.user-info-box{
		height: 180upx;
		display:flex;
		align-items:center;
		position:relative;
		z-index: 1;
		.portrait{
			width: 130upx;
			height: 130upx;
			border:5upx solid #fff;
			border-radius: 50%;
		}
		.username{
			font-size: $font-lg + 6upx;
			color: $font-color-dark;
			margin-left: 20upx;
		}
	}

	.vip-card-box{
		display:flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0,0,0,.7), rgba(0,0,0,.8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;
		.card-bg{
			position:absolute;
			top: 20upx;
			right: 0;
			width: 380upx;
			height: 260upx;
			opacity: .2;
		}
		.b-btn{
			position: absolute;
			right: 20upx;
			top: 16upx;
			width: 132upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 22upx;
			color: #36343c;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
		}
		.tit{
			font-size: $font-base+2upx;
			color: #f7d680;
			margin-bottom: 28upx;
			.yticon{
				color: #f6e5a3;
				margin-right: 16upx;
			}
		}
		.e-b{
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}
	.cover-container{
		background: $page-color-base;
		margin-top: -150upx;
		padding: 0 30upx;
		position:relative;
		background: #f5f5f5;
		padding-bottom: 20upx;
		.arc{
			position:absolute;
			left: 0;
			top: -34upx;
			width: 100%;
			height: 36upx;
		}
	}
	.tj-sction{
		@extend %section;
		.tj-item{
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #75787d;
		}
		.num{
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}
	.order-section{
		@extend %section;
		padding: 32upx 0;
		margin-top: 20upx;
		.order-item{
			@extend %flex-center;
			font-size: $font-sm;
			color: $font-color-dark;
		}
		.yticon{
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #fa436a;
		}
		.icon-shouhoutuikuan{
			font-size:44upx;
		}
	}
	
	.list-cell{
		display:flex;
		align-items:baseline;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-base;
			color: $font-color-light;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
	}
</style>